<?php
session_start();
$idUser =  1;

include ($_SERVER['DOCUMENT_ROOT']."/wewant1/includes/db_exe/db_connection.php");

if( isset($_POST['idShop'])&&($_POST['resource']==='Shop')){

	$idShop = mysql_real_escape_string($_POST['idShop']);
	//kiem tra idshop co ton tai khong, nguoi duoc follow la shop hay la user
	$idFollow = substr(md5($idShop.'-'.'1'.'-0'),0,10);//tao id cho event follow cua user va shop idshop-iduser-0 (0 la shop source)

	$db = connectDb();
	$q = "SELECT id_follow FROM follows WHERE id_follow = '{$idFollow}'";
	$r  = exeQuery($db,$q);
	if(mysqli_num_rows($r)==0){
		//kiem tra neu chua follow thi moi tien hanh Insert Db
		$q = "INSERT INTO follows (id_follow, id_user_follow, id_followed, shop_or_user_followed, created_date)";
		$q .= " VALUES ('{$idFollow}',{$idUser},{$idShop},0,NOW())";
		
		exeQuery($db,$q);
		if(mysqli_affected_rows($db)==1){
			echo 1;//1 la insert success
		}
	}else{
		$q = "DELETE FROM follows WHERE id_follow = '{$idFollow}' LIMIT 1";
		exeQuery($db,$q);
		if(mysqli_affected_rows($db)==1){
			echo 0;//0 la delete database success
		}
		

	}
}

?>